package com.bjesc_b2c.app.appraiset.mvp.view.activity;

import android.content.Intent;
import android.support.annotation.IdRes;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.bjesc_b2c.app.appraiset.R;
import com.bjesc_b2c.app.appraiset.adapter.AudioInfoAdapter;
import com.bjesc_b2c.app.appraiset.base.BaseActivity;
import com.bjesc_b2c.app.appraiset.bean.AudiOrderBean;
import com.bjesc_b2c.app.appraiset.http.IApiActivityRequest;
import com.bjesc_b2c.app.appraiset.utils.CustomSharedPreferences;
import com.rxretrofit_build.util.MapUtil;

import java.util.HashMap;

import butterknife.Bind;
import butterknife.OnClick;
import rxretrofit_base.model.KeyBean;
import rxretrofit_base.model.ShowType;
import rxretrofit_base.model.TokenBean;
import rxretrofit_base.net.BaseSubscriber;
import rxretrofit_base.net.RxRequest;

/**
 * Created by yidong on 2017/8/31.
 */

public class AudioOrderInfo extends BaseActivity implements RadioGroup.OnCheckedChangeListener {
    private String dispatch_id;
    @Bind(R.id.tv_tou_id)
    TextView touIdTv;
    @Bind(R.id.tv_tou_name)
    TextView touNameTv;
    @Bind(R.id.tv_sex)
    TextView touSexTv;
    @Bind(R.id.tv_tou_phone)
    TextView touPhoneTv;
    @Bind(R.id.tv_tou_level)
    TextView touLevelTv;
    @Bind(R.id.tv_installment_demand)
    TextView installmentTv;
    @Bind(R.id.tv_time_demand)
    TextView buyCarTimeTv;
    @Bind(R.id.tv_city_demand)
    TextView cityTv;
    @Bind(R.id.tv_model_demand)
    TextView demandBrandTv;
    @Bind(R.id.tv_price_demand)
    TextView demandPriceTv;
    @Bind(R.id.tv_tou_source)
    TextView tou_SourceTv;
    @Bind(R.id.tv_dispatch_id)
    TextView dispatchIdTv;
    @Bind(R.id.tv_create_dispatch_name)
    TextView createNameTv;
    @Bind(R.id.tv_create_time)
    TextView createTimeTv;
    @Bind(R.id.tv_accept_name)
    TextView acceptNameTv;
    @Bind(R.id.tv_car_id)
    TextView demandCarIdTv;
    @Bind(R.id.tv_one_status)
    TextView oneStatusTv;
    @Bind(R.id.tv_car_model)
    TextView carModelTv;
    @Bind(R.id.tv_car_price)
    TextView carPriceTv;
    @Bind(R.id.tv_fail_time)
    TextView failTimeTv;
    @Bind(R.id.tv_remarks)
    TextView remarksTv;
    @Bind(R.id.tv_title)
    TextView titleTv;
    @Bind(R.id.recycler_view)
    RecyclerView recyclerView;
    @Bind(R.id.cb_score_one)
    RadioButton checkBoxOne;
    @Bind(R.id.cb_score_two)
    RadioButton checkBoxTwo;
    @Bind(R.id.cb_score_three)
    RadioButton checkBoxThree;
    @Bind(R.id.radio_group)
    RadioGroup mrRadioGroup;
    @Bind(R.id.ed_audio_content)
    EditText audioContent;
    @Bind(R.id.linear_bottom)
    LinearLayout linearBottom;
    @Bind(R.id.btn_distribution)
    Button distributionBtn;
    private AudiOrderBean mOrderBean;
    private int clueType;

    @Override
    protected int getLayout() {
        return R.layout.activity_audit_order_info;
    }
    @Override
    protected void initEventAndData() {
        titleTv.setText("订单详情");
        dispatch_id = CustomSharedPreferences.getString(this, KeyBean.ORDER_ID.valueOf(), KeyBean.FILE_NAME.valueOf());
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
        mrRadioGroup.setOnCheckedChangeListener(this);
        clueType = getIntent().getIntExtra("clueType",1);
        if (clueType != 1){
            linearBottom.setVisibility(View.GONE);
            audioContent.setVisibility(View.GONE);
            if (clueType == 4){
                distributionBtn.setVisibility(View.VISIBLE);
            }
        }
    }
    @Override
    protected void createPresenter() {
        getDisInfo(dispatch_id);
    }
    @Override
    public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
        switch (checkedId) {
            case R.id.cb_score_one:
                if (checkBoxOne.isChecked()) {
                    recyclerView.setVisibility(View.VISIBLE);
                    checkBoxOne.setChecked(false);
                    checkBoxOne.setTextColor(getResources().getColor(R.color.colorFFFF7900));
                    if (mOrderBean != null) {
                        AudioInfoAdapter ada = new AudioInfoAdapter(mContext, R.layout.layout_item_toutrail, mOrderBean.getTou_trail());
                        recyclerView.setAdapter(ada);
                    }
                } else {
                    checkBoxOne.setTextColor(getResources().getColor(R.color.colorFF030303));
                    mrRadioGroup.clearCheck();
                    recyclerView.setVisibility(View.GONE);
                }
                checkBoxThree.setChecked(false);
                checkBoxTwo.setChecked(false);
                checkBoxThree.setTextColor(getResources().getColor(R.color.colorFF030303));
                checkBoxTwo.setTextColor(getResources().getColor(R.color.colorFF030303));
                break;
            case R.id.cb_score_two:
                if (checkBoxTwo.isChecked()) {
                    recyclerView.setVisibility(View.VISIBLE);
                    checkBoxTwo.setChecked(false);
                    checkBoxTwo.setTextColor(getResources().getColor(R.color.colorFFFF7900));
                    if (mOrderBean != null) {
                        AudioInfoAdapter ada = new AudioInfoAdapter(mContext, R.layout.layout_item_toutrail, mOrderBean.getDispatch_trail());
                        recyclerView.setAdapter(ada);
                    }
                } else {
                    mrRadioGroup.clearCheck();
                    recyclerView.setVisibility(View.GONE);
                    checkBoxTwo.setTextColor(getResources().getColor(R.color.colorFF030303));
                }
                checkBoxThree.setChecked(false);
                checkBoxOne.setChecked(false);
                checkBoxThree.setTextColor(getResources().getColor(R.color.colorFF030303));
                checkBoxOne.setTextColor(getResources().getColor(R.color.colorFF030303));
                break;
            case R.id.cb_score_three:
                if (checkBoxThree.isChecked()) {
                    recyclerView.setVisibility(View.VISIBLE);
                    checkBoxThree.setChecked(false);
                    checkBoxThree.setTextColor(getResources().getColor(R.color.colorFFFF7900));
                    if (mOrderBean != null) {
                        AudioInfoAdapter ada = new AudioInfoAdapter(mContext, R.layout.layout_item_toutrail, mOrderBean.getLike_cars());
                        recyclerView.setAdapter(ada);
                    }
                } else {
                    mrRadioGroup.clearCheck();
                    recyclerView.setVisibility(View.GONE);
                    checkBoxThree.setTextColor(getResources().getColor(R.color.colorFF030303));
                }
                checkBoxTwo.setChecked(false);
                checkBoxOne.setChecked(false);
                checkBoxTwo.setTextColor(getResources().getColor(R.color.colorFF030303));
                checkBoxOne.setTextColor(getResources().getColor(R.color.colorFF030303));
                break;
        }
    }

    private void examineDispatch(String dispatchID,int type) {
        HashMap<String, Object> build = new MapUtil().put("dispatch_id", dispatchID).
                put("examine_status", type).
                put("content",audioContent.getText().toString()).
                build();
        RxRequest.request(ShowType.DISMISS, RxRequest.getClient(mContext, build).create(IApiActivityRequest.class).examineDispatch()
                , new BaseSubscriber<TokenBean>(mContext) {
                    @Override
                    public void onSuccess(TokenBean o) {

                    }
                    @Override
                    public void onFailure(Throwable e) {
                        cancelLoading();
                    }

                    @Override
                    public void onStart() {
                        showLoading();
                    }

                    @Override
                    public void onCompleted() {
                        cancelLoading();
                    }
                });
    }
    private void getDisInfo(String dispatchID) {
        HashMap<String, Object> build = new MapUtil().put("dispatch_id", dispatchID).build();
        RxRequest.request(ShowType.DISMISS, RxRequest.getClient(mContext, build).create(IApiActivityRequest.class).getDisfailInfo()
                , new BaseSubscriber<TokenBean<AudiOrderBean>>(mContext) {
                    @Override
                    public void onSuccess(TokenBean<AudiOrderBean> o) {
                        if (o.getData() != null)
                            setDate(o.getData());
                    }

                    @Override
                    public void onFailure(Throwable e) {
                        cancelLoading();
                    }

                    @Override
                    public void onStart() {
                        showLoading();
                    }

                    @Override
                    public void onCompleted() {
                        cancelLoading();
                    }
                });
    }

    private void setDate(AudiOrderBean orderBean) {
        mOrderBean = orderBean;
        checkBoxTwo.setChecked(true);
        touIdTv.setText(orderBean.getTour_info().getTou_id());
        touNameTv.setText(orderBean.getTour_info().getName());
        touSexTv.setText(orderBean.getTour_info().getSex());
        touPhoneTv.setText(orderBean.getTour_info().getPhone());
        touLevelTv.setText(orderBean.getTour_info().getTour_level());
        installmentTv.setText(orderBean.getTour_info().getInstallment_demand());
        buyCarTimeTv.setText(orderBean.getTour_info().getTime_demand());
        demandPriceTv.setText(orderBean.getTour_info().getPrice_demand());
        cityTv.setText(orderBean.getTour_info().getCity_demand());
        demandBrandTv.setText(orderBean.getTour_info().getModel_demand());
        tou_SourceTv.setText(orderBean.getTour_info().getSource());
        dispatchIdTv.setText(orderBean.getDispatch_info().getDispatch_id());
        createNameTv.setText(orderBean.getDispatch_info().getCreate_dispatch_name());
        createTimeTv.setText(orderBean.getDispatch_info().getAdd_time());
        acceptNameTv.setText(orderBean.getDispatch_info().getAccept_name());
        demandCarIdTv.setText(orderBean.getDispatch_info().getCarInfo().getCar_id());
        oneStatusTv.setText(orderBean.getDispatch_info().getCarInfo().getOne_status());
        carModelTv.setText(orderBean.getDispatch_info().getCarInfo().getTitle());
        carPriceTv.setText(orderBean.getDispatch_info().getCarInfo().getOwner_price());
        failTimeTv.setText(orderBean.getDispatch_info().getComplete_time());
        remarksTv.setText(orderBean.getDispatch_info().getConent());
    }

    @OnClick({R.id.iv_return,R.id.btn_audio_pass,R.id.btn_audio_no_pass,R.id.btn_distribution})
    public void OnClick(View v) {
        switch (v.getId()) {
            case R.id.iv_return:
                finish();
                break;
            case R.id.btn_audio_pass:
                examineDispatch(dispatch_id,1);
                break;
            case R.id.btn_audio_no_pass:
                examineDispatch(dispatch_id,2);
                break;
            case R.id.btn_distribution:
                Intent mIntent = new Intent(mContext,DistributionActivity.class);
                mIntent.putExtra("TYPE",1);
                mIntent.putExtra("dispatchId",dispatch_id);
                startIntent(mIntent,false);
                break;
        }

    }

}
